﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace tactics
{
    using DisplayOptions = Dictionary<string, Utility.Pair<int, int>>;
    using KeyOptions = Dictionary<string, Microsoft.Xna.Framework.Input.Keys>;
    
    public class ConfigManager
    {
        DisplayOptions DisplayConfig;
        KeyOptions InputConfig;
        string displayMode;

        #region Properties

        public string DisplayMode
        {
            get { return displayMode; }
        }
        
        public bool Fullscreen
        {
            get { return (displayMode == "Fullscreen") ? true : false; }
        }
        
        public DisplayOptions Display
        {
            get { return DisplayConfig; }
        }

        public KeyOptions Keys
        {
            get { return InputConfig; }
        }

        #endregion

        public ConfigManager(string configFile)
        {
            DisplayConfig = new DisplayOptions();
            InputConfig = new KeyOptions();

            Parse(configFile);
        }

        public void ToggleFullscreen()
        {
            if (displayMode == "Fullscreen")
                displayMode = "Window";
            else
                displayMode = "Fullscreen";
        }

        #region Parsing Infrastructure

        private void Parse(string configFile)
        {
            if (!System.IO.File.Exists(configFile))
                CreateConfigFromDefault(configFile);

            XmlDocument doc = new XmlDocument();
            doc.Load(configFile);

            // check if this is a proper xml file (for configuration)
            XmlNodeList name = doc.GetElementsByTagName("Config");
            if (name.Count == 0)
                return;

            XmlNodeList displayConfig = doc.GetElementsByTagName("Display");
            foreach (XmlNode displayNode in displayConfig) // should only be one of these!
            {
                displayMode = displayNode.Attributes["Mode"].Value;
                foreach (XmlNode node in displayNode)
                {
                    string displayName = node.Name;
                    DisplayConfig[displayName] = new Utility.Pair<int, int>();
                    DisplayConfig[displayName].First = int.Parse(node.Attributes["Width"].Value);
                    DisplayConfig[displayName].Second = int.Parse(node.Attributes["Height"].Value);
                }
            }

            XmlNodeList inputConfig = doc.GetElementsByTagName("Input");
            foreach (XmlNode inputNode in inputConfig)
            {
                foreach (XmlNode node in inputNode)
                {
                    string functionName = node.Name;
                    InputConfig[functionName] = (Microsoft.Xna.Framework.Input.Keys)Enum.Parse(typeof(Microsoft.Xna.Framework.Input.Keys), node.Attributes["Key"].Value);
                }
            }
        }

        private void CreateConfigFromDefault(string configFile)
        {
            // if no UserConfig file exists, we make a new one from the default
            System.IO.File.Copy("DefaultConfig.xml", "UserConfig.xml");
        }

        #endregion
    }
}